<?php
class StatsController extends AppController
{
    public $name = 'Stats';
    public $helpers = array('Html', 'Form', 'GChart.GChart');
    public $components = array('Session');

    public $uses = array(
        'tbl_account', 'tbl_character', 'tbl_billing_log', 'tbl_item', 'tbl_card', 'tbl_log_mcu',
        'tbl_gem_log', 'tbl_log_dau'
    );

    public function stats_user()
    {
        if(!CakeSession::check('tbl_auth'))
        {
            if(!empty($this->passedArgs['id']))
            {
                $this->redirect(array(
                    'controller' => 'User',
                    'action' => 'user_select',
                    'id' => $this->passedArgs['id'],
                    'search_value' => $this->passedArgs['search_value'],
                ));
            }

            $this->redirect(array(
                'controller' => 'User',
                'action' => 'user_list',
            ));
        }

        $get_year = empty($_GET['year'])? date('Y', time()) : $_GET['year'];
        $get_month = empty($_GET['month']) ? date('m', time()) : $_GET['month'];

        $total_account = $this->tbl_account->get_total_count();
        $end_day = date("t", mktime(0, 0, 0, $get_month, 1, $get_year));

        $total_sales = 0;
        $total_sales_user = 0;

        for($i = 1; $i <= $end_day; ++$i)
        {
            $day = str_pad($i,'2','0',STR_PAD_LEFT);
            $NRU = $this->__NRU($get_year . '-' . $get_month . '-' . $day);
            $DAU = $this->__DAU($get_year . '-' . $get_month . '-' . $day);
            $BILLING = $this->__BILLING($get_year . '-' . $get_month . '-' . $day);
            $SALES = $this->__SALES($get_year . '-' . $get_month . '-' . $day);

            $user_value[$i][0] = $get_year . '-' . $get_month . '-' . $day;
            $user_value[$i][1] = number_format($NRU);
            $user_value[$i][2] = number_format($DAU);
            $user_value[$i][3] = $BILLING;
            $user_value[$i][4] = number_format($SALES);

            $total_sales_user += $BILLING;
            $total_sales += $SALES;
        }

        $ARPPU = 0;

        if( !empty($total_sales))
            $ARPPU = round(($total_sales/$total_sales_user), 2);

        $static_data = array(
            'labels' => array(
                array('string' => __('DATE')),
                array('string' => __('NRU')),
                array('string' => __('DAU')),
                array('string' => __('BILLING')),
                array('string' => __('SALES'))
            ),
            'data' => $user_value,
            'width' => 550,
            'height' => 710,
            'type' => 'table'
        );

        $MCU = $this->__MCU();
        $this->set(compact('static_data', 'total_account', 'total_sales', 'ARPPU', 'MCU'));
    }

    public function stats_user_log()
    {
        $today = $_GET['today'];

        $BILLING_LIST = $this->__BILLING_LIST($today);

        $total_sales = 0;
        $user_value = array();

        foreach($BILLING_LIST as $num => $value)
        {
            $nick_name = $this->tbl_account->get_account($value['tbl_billing_log']['a2x_id']);
            $user_value[$num][0] = $nick_name['tbl_account']['nickname'];
            $user_value[$num][1] = $value['tbl_billing_log']['order_id'];
            $user_value[$num][2] = $value['tbl_billing_log']['cash'];
            $user_value[$num][3] = $value['tbl_billing_log']['reg_date'];

            $total_sales += $value['tbl_billing_log']['cash'];
        }

        $static_data = array(
            'labels' => array(
                array('string' => __('Id')),
                array('string' => __('Order Id')),
                array('string' => __('Cash')),
                array('string' => __('Date'))
            ),
            'data' => $user_value,
            'width' => 550,
            'height' => 710,
            'type' => 'table'
        );

        $this->set(compact('static_data', 'total_sales'));
    }

    public function stats_level()
    {
        $level_list = $this->tbl_character->get_char_level();
        $char_total = $this->tbl_character->get_char_total();

        foreach($level_list as $num => $value)
        {
            $level_data[$num][0] = $value['tbl_character']['level'];
            $level_data[$num][1] = $value[0]['count'];
            $level_data[$num][2] = round(($value[0]['count']/$char_total)*100, 2) . '%';
        }

        $static_data = array(
            'labels' => array(
                array('string' => __('LEVEL')),
                array('string' => __('COUNT')),
                array('string' => __('RATE')),
            ),
            'data' => $level_data,
            'width' => 400,
            'height' => 500,
            'type' => 'table'
        );

        $this->set(compact('static_data', 'char_total'));
    }

    public function stats_gem()
    {
        $gem_data = $this->tbl_gem_log->get_gem_use();
        $gem_total = $this->tbl_gem_log->get_gem_use_total();

        foreach($gem_data as $num => $value)
        {
            switch($value['tbl_gem_log']['gem_use_type'])
            {
                case 1:
                    $gem_log[$num][0] = "캐릭터 부활";
                    break;
                case 2:
                    $gem_log[$num][0] = "오브 슬롯1 확장";
                    break;
                case 3:
                    $gem_log[$num][0] = "오브 슬롯2 확장";
                    break;
                case 4:
                    $gem_log[$num][0] = "에너지 구입";
                    break;
                case 5:
                    $gem_log[$num][0] = "골드 구입";
                    break;
                case 6:
                    $gem_log[$num][0] = "상점 아이템 구매";
                    break;
                case 7:
                    $gem_log[$num][0] = "상점 갱신";
                    break;
                case 8:
                    $gem_log[$num][0] = "용병 갱신";
                    break;
                case 9:
                    $gem_log[$num][0] = "던전 물약 사용";
                    break;
                case 10:
                    $gem_log[$num][0] = "카드 슬롯1 확장";
                    break;
                case 11:
                    $gem_log[$num][0] = "카드 슬롯2 확장";
                    break;
                case 12:
                    $gem_log[$num][0] = "캐릭터 슬롯1 확장";
                    break;
                case 13:
                    $gem_log[$num][0] = "캐릭터 슬롯2 확장";
                    break;
                case 14:
                    $gem_log[$num][0] = "친구 깨우기";
                    break;
                case 15:
                    $gem_log[$num][0] = "재도전";
                    break;
                case 16:
                    $gem_log[$num][0] = "카드조합";
                    break;
                case 17:
                    $gem_log[$num][0] = "용병 고용";
                    break;
                case 18:
                    $gem_log[$num][0] = "자신 부활";
                    break;
                default:
                    break;
            }
            $gem_log[$num][1] = $value[0]['count'];
            $gem_log[$num][2] = $value[0]['sum'];
            $gem_log[$num][3] = round(($value[0]['sum']/$gem_total[0]['sum'])*100, 2) . '%';
        }

        $static_data = array(
            'labels' => array(
                array('string' => __('사용 목록')),
                array('string' => __('사용 횟수')),
                array('string' => __('보석 수량')),
                array('string' => __('사용 비율'))
            ),
            'data' => $gem_log,
            'width' => 500,
            'height' => 450,
            'type' => 'table'
        );

        $this->set(compact('static_data'));

    }

    private function __NRU($_date)
    {
        return $this->tbl_account->get_nru($_date);
    }

    private function __DAU($_date)
    {
        return $this->tbl_log_dau->get_dau($_date);
    }

    private function __MCU()
    {
        return $this->tbl_log_mcu->get_mcu();
    }

    private function __BILLING($_date)
    {
        return $this->tbl_billing_log->get_today_billing($_date);
    }

    private function __BILLING_LIST($_date)
    {
        return $this->tbl_billing_log->get_today_billing_list($_date);
    }

    private function __SALES($_date)
    {
        $data = $this->tbl_billing_log->get_today_sales($_date);
        $cash = 0;

        if(!empty($data))
        {
            foreach($data as $value)
            {
                $cash += $value['tbl_billing_log']['cash'];
            }
        }

        return $cash;
    }
}